/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Tela.java
 *
 * Created on 11/06/2009, 00:35:52
 */

package talkunix;
import java.io.*;

import java.awt.Frame;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author Administrador
 */
public class Tela extends javax.swing.JFrame {
    private About janelaAbout;
    private Frame Tela;
    private Conexao janelaConexao;
    public String Usuario;
    public String Outro;


    

    /** Creates new form Tela */
    public Tela() {
        initComponents();
        Usuario = ClienteServidor.getUsuario()+": ";
        Outro = "Outro: ";
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        EditaTexto = new java.awt.TextField();
        ButaoEnvia = new java.awt.Button();
        ButaoConexao = new javax.swing.JButton();
        jButaoAbout = new javax.swing.JButton();
        jButaoOuvir = new javax.swing.JButton();
        Exibidor = new java.awt.List();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Mega-Talk");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setMinimumSize(new java.awt.Dimension(300, 400));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        EditaTexto.setText("Digite aqui:");
        EditaTexto.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                Apertaenter(evt);
            }
        });

        ButaoEnvia.setLabel("Envia");
        ButaoEnvia.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ButaoEnviaMouseClicked(evt);
            }
        });

        ButaoConexao.setText("Conexao");
        ButaoConexao.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ConexaoMouseClicked(evt);
            }
        });

        jButaoAbout.setText("About");
        jButaoAbout.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                AboutMouseClicked(evt);
            }
        });

        jButaoOuvir.setText("Ouvir");
        jButaoOuvir.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButaoOuvirMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(ButaoConexao)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButaoOuvir)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButaoAbout)
                .addGap(210, 210, 210))
            .addGroup(layout.createSequentialGroup()
                .addComponent(EditaTexto, javax.swing.GroupLayout.PREFERRED_SIZE, 297, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20)
                .addComponent(ButaoEnvia, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(Exibidor, javax.swing.GroupLayout.DEFAULT_SIZE, 522, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ButaoConexao)
                    .addComponent(jButaoOuvir)
                    .addComponent(jButaoAbout))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Exibidor, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(EditaTexto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ButaoEnvia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void AboutMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_AboutMouseClicked
       ChamaAbout();
    }//GEN-LAST:event_AboutMouseClicked

    private void ConexaoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ConexaoMouseClicked
       ChamaConexao();
    }//GEN-LAST:event_ConexaoMouseClicked

    private void jButaoOuvirMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButaoOuvirMouseClicked
        try {
            try {
                ClienteServidor.Ouvir();
            } catch (InterruptedException ex) {
                ClienteServidor.deuErro("Deu Erro!");
            }
        } catch (IOException ex) {
            ClienteServidor.deuErro("Deu Erro!");
        }

    }//GEN-LAST:event_jButaoOuvirMouseClicked

    private void ButaoEnviaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ButaoEnviaMouseClicked
       Enviar();
    }//GEN-LAST:event_ButaoEnviaMouseClicked

    private void Apertaenter(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_Apertaenter
         if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
                     Enviar();
                 }
    }//GEN-LAST:event_Apertaenter

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        try {
            ClienteServidor.Fechar();
        } catch (IOException ex) {
            Logger.getLogger(Tela.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_formWindowClosed


//     private void EsperarEnter() {
//
//         KeyAdapter keyAdapter = new KeyAdapter() {
//
//           public void keyPressed(KeyEvent evt) {
//
//                 if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
//                     Enviar();
//                 }
//             }
//         } ;
//     }


    public void Enviar()
    {
        String Texto;
        String Textoenvia;
        Texto = EditaTexto.getText();
        EditaTexto.setText("");
        Textoenvia = Texto;
        Texto = Usuario +Texto;
        Exibidor.add(Texto);



        try {
            System.out.println("Tentando enviar");
            ClienteServidor.Enviar(Textoenvia);
        } catch (IOException ex) {
            ClienteServidor.deuErro("Deu Erro!");
        }
    }



    private void IniciaTexto()
    {
       EditaTexto.setText("");
    }

    public void ChamaConexao()
    {
            janelaConexao = new Conexao();
            janelaConexao.setVisible(true);

    }

    public void ChamaAbout()
    {

            janelaAbout = new About();
            janelaAbout.setVisible(true);
            
    }

    public void Display(String texto)
    {
            texto = Outro + texto;
            Exibidor.add(texto);
    }

    /**
    * @param args the command line arguments
    */
/*
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Tela().setVisible(true);
            }



        });
    }
 */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ButaoConexao;
    private java.awt.Button ButaoEnvia;
    private java.awt.TextField EditaTexto;
    private java.awt.List Exibidor;
    private javax.swing.JButton jButaoAbout;
    private javax.swing.JButton jButaoOuvir;
    // End of variables declaration//GEN-END:variables

}
